import router from "@/router";
import Nprogress from "nprogress";
import type { RouteLocationNormalized, NavigationGuardNext } from "vue-router";
import { getUserInfo } from "@/utils/localStorage.ts";

router.beforeEach((to: RouteLocationNormalized, from: RouteLocationNormalized, next: NavigationGuardNext): void => {
   Nprogress.start();
   const userInfo = getUserInfo();
   if (userInfo) {
      next();
   } else {
      if (to.path.startsWith("/member")) {
         next({ path: "/login", query: { redirectUrl: to.fullPath } });
         Nprogress.done();
      } else {
         next();
      }
   }
});

router.afterEach((to: RouteLocationNormalized, from: RouteLocationNormalized): void => {
   Nprogress.done();
});
